package ink.ykb.demo.permission.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ink.ykb.demo.config.Constant;
import ink.ykb.demo.permission.entity.Role;
import ink.ykb.demo.permission.entity.Rule;
import ink.ykb.demo.permission.service.dto.UserPermissionInfo;
import ink.ykb.demo.user.service.UserService;

/**
 * 用户权限判断相关服务
 * @author mading
 *
 */
@Service
public class UserPermissionServiceImpl implements UserPermissionService{
  
  @Autowired
  private UserService userService;

  @Override
  public boolean canAcessFunction(String funCode) {
    
    UserPermissionInfo currentUserPermissionInfo = userService.getCurrentUserPermissionInfo();
    if(currentUserPermissionInfo == null) {
      return false;
    }
    
    //如果是超级管理员，拥有所有权限
    List<Role> roles = currentUserPermissionInfo.getRoles();
    boolean isSuperAdmin = userService.isSuperAdmin(roles);
    if(isSuperAdmin) {
    	return true;
    }
    
    //判断是否有权限
    List<Rule> rules = currentUserPermissionInfo.getRules();
    for (Rule rule : rules) {
      if(rule.getCode().equalsIgnoreCase(funCode)) {
        return true;
      }
    }
    
    return false;
  }
 
}
